package com.zhou.core.http.client;

import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.zhou.util.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;

import com.zhou.core.http.client.impl.HttpUploadHandler;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public final class HttpClientService {
	
	public static void doUpload(String url,NameValuePair... paramters) throws Exception
	{
		new HttpUploadHandler().handle(url, paramters);
	}

	public static String doGet(String url,int timeout)
	{
		try {
			String result =  null;
			HttpClient client = HttpClients.createDefault();
			if (!StringUtils.isEmpty(url)) {
				HttpGet httpGet = new HttpGet(url);
				RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(timeout).setConnectTimeout(timeout).build();//设置请求和传输超时时间
				httpGet.setConfig(requestConfig);
				HttpResponse response = client.execute(httpGet);
				HttpEntity respEntity = response.getEntity();

				if (respEntity != null)
				{
					result = EntityUtils.toString(respEntity,"UTF-8");
				}
			}
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

}
